package com.yuncheng.spcyApi.flowController;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.yuncheng.vo.CurrentUserVo;
import com.yuncheng.vo.HttpResult;
import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.common.base.controller.BaseController;
import com.yuncheng.spcyApi.constant.common.BusinessConstant;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.entity.*;
import com.yuncheng.spcyApi.flow.JcyNdkpFlowService;
import com.yuncheng.spcyApi.flow.SpcyFlowConstant;
import com.yuncheng.spcyApi.mapper.SpcyKpNdkpBasicMapper;
import com.yuncheng.spcyApi.mapper.SpcyRsHcyMapper;
import com.yuncheng.spcyApi.service.*;
import com.yuncheng.spcyApi.utils.*;
import com.yuncheng.spcyApi.vo.common.OrderItemVo;
import com.yuncheng.spcyApi.vo.common.PageVo;
import com.yuncheng.spcyApi.vo.common.Result;
import com.yuncheng.spcyApi.vo.jcy.RsHcyVo;
import com.yuncheng.spcyApi.vo.ndkp.NdkpFlowMapVo;
import com.yuncheng.spcyApi.vo.ndkp.NdkpFlowVo;
import com.yuncheng.spcyApi.vo.spcy.SpcyReturnVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;
import utils.DateConversionUtils;
import utils.LogUtils;
import utils.ObjectToMapUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.*;
import java.util.stream.Collectors;

@Api(tags = "审评查验-年度考评流程接口")
@RestController
@RequestMapping("/api/spzx-spcy/flow-ndkp")
public class JcyNdkpFlowController extends BaseController<SpcyKpNdkpBasic, ISpcyKpNdkpBasicService> {
    private static final Logger log = LoggerFactory.getLogger(JcyNdkpFlowController.class);

    @Resource
    private SpcyKpNdkpBasicMapper spcyKpNdkpBasicMapper;

    @Resource
    private ISpcyKpNdkpBasicService spcyKpNdkpBasicService;

    @Resource
    private ISpcySqsxJobService spcySqsxJobService;

    @Resource
    private ISpcySqsxXpjlService spcySqsxXpjlService;

    @Resource
    private SpcyRsHcyMapper spcyRsHcyMapper;

    /*@Resource
    private NdkpConstant ndkpConstant;

    @Resource
    private NdkpFlowService ndkpFlowService;*/

    /*@Resource
    private LocalFileUploadUtils localFileUploadUtils;*/

    @Resource
    private MyExcelUtils myExcelUtils;

    @Resource
    @Lazy
    private ISpcyLogService spcyLogService;

    @Resource
    private JcyNdkpFlowService jcyNdkpFlowService;

    @Log(title = "流程监控-分页查询")
    @ApiOperation(value = "流程监控-分页查询")
    @GetMapping("flowJkPage")
    public HttpResult<?> flowJkPage(NdkpFlowVo flowVo, PageVo pageVo, OrderItemVo orderItemVo,
                                    @RequestParam(name = "onlyCurrentUser", defaultValue = "false") boolean onlyCurrentUser){

        String userid = null;
        if (onlyCurrentUser){
            CurrentUserVo user = GetCurrentUserUtils.getCurrentUser();
            if (user == null){
                return HttpResult.error("请重新登录，Token已失效！");
            }
        }
        flowVo.setFblzt(SpcyConstant.BLZT_DB);
        flowVo.setFlcmc(SpcyFlowConstant.NDKP_LCMC);
        Page<NdkpFlowVo> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());
        Page<NdkpFlowVo> list = spcyKpNdkpBasicMapper.selectFlowJobPage(page, userid, flowVo, orderItemVo);
        return HttpResult.ok(list);
    }


    @Log(title = "流程列表-分页查询")
    @ApiOperation(value = "流程列表-分页查询")
    @GetMapping("flowJobPage")
    public HttpResult<?> flowJobPage(NdkpFlowVo flowVo, PageVo pageVo, OrderItemVo orderItemVo,
                                     @RequestParam(name = "onlyCurrentUser", defaultValue = "true") boolean onlyCurrentUser){

        String userid = null;
        if (onlyCurrentUser){
            CurrentUserVo user = GetCurrentUserUtils.getCurrentUser();
            if (user == null){
                return HttpResult.error("请重新登录，Token已失效！");
            }
            userid = user.getId();
        }
        flowVo.setFblzt(SpcyConstant.BLZT_DB);
        flowVo.setFlcmc(SpcyFlowConstant.NDKP_LCMC);
        Page<NdkpFlowVo> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());
        Page<NdkpFlowVo> list = spcyKpNdkpBasicMapper.selectFlowJobPage(page, userid, flowVo, orderItemVo);
        return HttpResult.ok(list);
    }

    @Log(title = "我的已办流程列表-分页查询")
    @ApiOperation(value = "我的已办流程列表-分页查询")
    @GetMapping("flowMyYbJobPage")
    public HttpResult<?> flowMyYbJobPage(NdkpFlowVo flowVo, PageVo pageVo, OrderItemVo orderItemVo){

        String userid = GetCurrentUserUtils.getCurrentUserId();
        flowVo.setFblzt(SpcyConstant.BLZT_YB);
        flowVo.setFlcmc(SpcyFlowConstant.NDKP_LCMC);
        Page<NdkpFlowVo> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());
        Page<NdkpFlowVo> list = spcyKpNdkpBasicMapper.selectYbFlowJobPage(page, userid, flowVo, orderItemVo);

        /*if (CollectionUtil.isNotEmpty(list.getRecords())){
            list.setRecords(list.getRecords().stream().collect(Collectors.collectingAndThen(
                    Collectors.toCollection(
                            () -> new TreeSet<>(
                                    Comparator.comparing(o -> o.getNdkpid())
                            )
                    ), ArrayList<NdkpFlowVo>::new
            )));
//            list.setTotal(list.getRecords().size());
        }*/
        return HttpResult.ok(list);
    }

    @Log(title = "年度考评查询")
    @ApiOperation(value = "年度考评查询")
    @GetMapping("ndkpcxPageList")
    public HttpResult<?> ndkpcxPageList(NdkpFlowVo flowVo, PageVo pageVo, OrderItemVo orderItemVo){

        flowVo.setFlcmc(SpcyFlowConstant.NDKP_LCMC);
        Page<NdkpFlowVo> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());
        Page<NdkpFlowVo> list = spcyKpNdkpBasicMapper.selectNdkpcxPage(page, null, flowVo, orderItemVo);
        return HttpResult.ok(list);
    }

    @Log(title = "年度考评流程-保存接口")
    @ApiOperation(value = "年度考评流程-保存接口")
    @PostMapping(value = "/saveFlow")
    public HttpResult<?> saveFlow(@RequestBody NdkpFlowMapVo flowMapVo) {

        Result httpResult = jcyNdkpFlowService.handleFlow(flowMapVo, flowMapVo.getJobid(), null, SpcyFlowConstant.SUBMIT_TYPE_SAVE, flowMapVo.getOpinion());
        if (!httpResult.isSuccess()){
            return HttpResult.error(httpResult.getMessage());
        }

        return HttpResult.ok("保存成功");
    }

    @Log(title = "年度考评流程-提交接口")
    @ApiOperation(value = "年度考评流程-提交接口")
    @PostMapping(value = "/submitFlow")
    public HttpResult<?> submitFlow(@RequestBody NdkpFlowMapVo flowMapVo) {

        Result httpResult = jcyNdkpFlowService.handleFlow(flowMapVo, flowMapVo.getJobid(), null, SpcyFlowConstant.SUBMIT_TYPE_SUBMIT, flowMapVo.getOpinion());
        if (!httpResult.isSuccess()){
            return HttpResult.error(httpResult.getMessage());
        }

        return HttpResult.ok("提交成功");
    }

    @Log(title = "退回上一环节流程任务")
    @ApiOperation(value = "退回上一环节流程任务")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "jobid",value = "流程任务id",required = true),
            @ApiImplicitParam(name = "opinion",value = "意见",required = true)
    })
    @PostMapping(value = "returnUpperFlowJob")
    public HttpResult<?> returnUpperFlowJob(@RequestBody SpcyReturnVo spcyReturnVo){

        jcyNdkpFlowService.getReturnUpperFlowJob(spcyReturnVo);

        return HttpResult.ok("退回成功");
    }

    @Log(title = "批量办理-提交接口")
   /* @ApiOperation(value = "批量办理-提交接口")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(value = "jobids",name = "流程任务ids",required = true),
            @ApiImplicitParam(value = "smgflag",name = "是否发送短信",required = true),
            @ApiImplicitParam(value = "opinion",name = "办理意见",required = true)
    })*/
    @PostMapping(value = "batchSubmitFlow")
    public HttpResult<?> batchSubmitFlow(@RequestBody Map map){

        String submitType = SpcyFlowConstant.SUBMIT_TYPE_SUBMIT; // 提交
        String jobids = map.get("jobids").toString();
        String opinion = map.get("opinion").toString();
        String smgflag = map.get("smgflag").toString();

        if (StringUtils.isBlank(jobids)){
            return HttpResult.error("流程任务id参数丢失，请联系管理员");
        }

        if (StringUtils.isBlank(opinion)){
            return HttpResult.error("请填写办理意见");
        }

        List<String> jobidList = StringPatternUtils.getListByIds(jobids);
        List<SpcySqsxJob> jobList = spcySqsxJobService.listByIds(jobidList);
        if (CollectionUtils.isNotEmpty(jobidList)) {
            NdkpFlowMapVo ndkpFlowMapVo = null;
            for (SpcySqsxJob job : jobList) {
                ndkpFlowMapVo = new NdkpFlowMapVo();
                ndkpFlowMapVo.setDbrid("");
                ndkpFlowMapVo.setJobid(job.getId());
                ndkpFlowMapVo.setNdkpid(job.getFpid());
                ndkpFlowMapVo.setOpinion(opinion);
                ndkpFlowMapVo.setSmgflag(smgflag);

//                Result httpResult1 = ndkpFlowService.handleFlow(ndkpFlowMapVo, job, job.getId(), job.getFpid(), submitType, opinion);
                Result httpResult1 = jcyNdkpFlowService.handleFlow(ndkpFlowMapVo, job, job.getId(), job.getFpid(), submitType, opinion);

                if (!httpResult1.isSuccess()) {
                    return HttpResult.error(httpResult1.getMessage());
                }
            }
        }
        return HttpResult.ok("批量提交成功");
    }

    @Log(title = "查询当前流程任务详情-通过当前流程任务id")
    @ApiOperation(value = "查询当前流程任务详情-通过当前流程任务id")
    @ApiImplicitParam(name = "jobid",value = "流程任务id",required = true)
    @GetMapping(value = "queryJobByJobId")
    public HttpResult<?> queryJobByJobId(@RequestParam("jobid")String jobid){
        if (StringUtils.isBlank(jobid)){
            return HttpResult.error("流程任务id不能为空");
        }

        return HttpResult.ok(jcyNdkpFlowService.getJobConcernByJobId(jobid));
    }

    @Log(title = "分页查询个人的年度考评基本信息")
//    @ApiOperation(value = "分页查询个人的年度考评基本信息")
    @GetMapping("queryPageList")
    public HttpResult<?> queryPageList(SpcyKpNdkpBasic ndkpBasic,PageVo pageVo,OrderItemVo orderItemVo,
                                       @RequestParam(name = "onlyCurrentUser", defaultValue = "true") boolean onlyCurrentUser,
                                       @RequestParam(name = "sfcxgr",defaultValue = "")String sfcxgr){
        String userid = "";
        if (StringUtils.isNotBlank(sfcxgr) && sfcxgr.equals("否")){
            onlyCurrentUser = false;
        }
        if (onlyCurrentUser) {
            CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
            if (currentUser == null){
                return HttpResult.error("登录已失效，请重新登录");
            }
            userid = currentUser.getId();
        }
        Page<SpcyKpNdkpBasic> page = new Page<>(pageVo.getPageNo(),pageVo.getPageSize());

        LambdaQueryWrapper<SpcyKpNdkpBasic> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(ndkpBasic.getFywflcode()),SpcyKpNdkpBasic::getFywflcode,ndkpBasic.getFywflcode());
        queryWrapper.like(StringUtils.isNotBlank(userid),SpcyKpNdkpBasic::getFuserid,userid);
        queryWrapper.like(StringUtils.isNotBlank(ndkpBasic.getFsftjkh()),SpcyKpNdkpBasic::getFsftjkh,ndkpBasic.getFsftjkh());
        queryWrapper.like(StringUtils.isNotBlank(ndkpBasic.getFyear()),SpcyKpNdkpBasic::getFyear,ndkpBasic.getFyear());
        queryWrapper.like(StringUtils.isNotBlank(ndkpBasic.getFusermc()),SpcyKpNdkpBasic::getFusermc,ndkpBasic.getFusermc());
        queryWrapper.like(StringUtils.isNotBlank(ndkpBasic.getFgzdw()),SpcyKpNdkpBasic::getFgzdw,ndkpBasic.getFgzdw());
        queryWrapper.like(StringUtils.isNotBlank(ndkpBasic.getFsfcykp()),SpcyKpNdkpBasic::getFsfcykp,ndkpBasic.getFsfcykp());
        queryWrapper.like(StringUtils.isNotBlank(ndkpBasic.getFndzhkpjg()),SpcyKpNdkpBasic::getFndzhkpjg,ndkpBasic.getFndzhkpjg());
        queryWrapper.like(StringUtils.isNotBlank(ndkpBasic.getFdqssk()),SpcyKpNdkpBasic::getFdqssk,ndkpBasic.getFdqssk());
        queryWrapper.like(StringUtils.isNotBlank(ndkpBasic.getFgrndzjqk()),SpcyKpNdkpBasic::getFgrndzjqk,ndkpBasic.getFgrndzjqk());

        if (StringUtils.isBlank(ndkpBasic.getFlczt())) {

        }else if (ndkpBasic.getFlczt().equals(SpcyFlowConstant.LCZT_LCBJ)){
            queryWrapper.like(StringUtils.isNotBlank(ndkpBasic.getFlczt()), SpcyKpNdkpBasic::getFlczt, SpcyFlowConstant.LCZT_LCBJ);
        }else {
            queryWrapper.isNull(SpcyKpNdkpBasic::getFlczt);
        }
        if (ndkpBasic.getFzjkssj() != null){
            queryWrapper.le(SpcyKpNdkpBasic::getFzjkssj,ndkpBasic.getFzjkssj());
        }
        if (ndkpBasic.getFzjjssj() != null){
            queryWrapper.ge(SpcyKpNdkpBasic::getFzjjssj,ndkpBasic.getFzjjssj());
        }

        if (StringUtils.isNotBlank(orderItemVo.getOrderItem())){
            OrderItemUtils.setOrderItem(queryWrapper, orderItemVo);
        }
        Page<SpcyKpNdkpBasic> basicPage = spcyKpNdkpBasicService.page(page, queryWrapper);
        return HttpResult.ok(basicPage);
    }

    @Log(title = "获取年度选派记录")
//    @ApiOperation(value = "获取年度选派记录")
    @GetMapping(value = "queryYearXpjl")
    public HttpResult<?> queryYearXpjl(@RequestParam(value = "fyear",defaultValue = "")String fyear,
                                       @RequestParam(value = "fywflcode",defaultValue = "")String fywflcode){
        Map map = new HashMap();

        LambdaQueryWrapper<SpcySqsxXpjl> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(fyear)) {
            queryWrapper.apply("to_char(fzpsj,'yyyy') = {0}", fyear);
        }
        List<SpcySqsxXpjl> list = spcySqsxXpjlService.list(queryWrapper);
        map.put("xpjlList",list);

        SpcyKpNdkpBasic ndkp = jcyNdkpFlowService.getNdXpTj(fywflcode,fyear);
        map.put("ndkp",ndkp);
        return HttpResult.ok(map);
    }

    @Log(title = "删除年度考评流程")
    /*@ApiOperation(value = "删除年度考评流程")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "jobids",value = "流程任务id,逗号分割",required = true)
    })*/
    @DeleteMapping(value = "deleteNdkpFlowJob")
    public HttpResult<?> deleteNdkpFlowJob(@RequestParam(value = "jobids",defaultValue = "")String jobids){
        if (StringUtils.isBlank(jobids)){
            return HttpResult.error("参数不能为空");
        }
        List<String> jobIdList = Arrays.asList(jobids.split(","));
        String logNr = "";
        Map<String, Object> map = new HashMap<>();
        if (CollectionUtils.isNotEmpty(jobIdList)){
            List<SpcySqsxJob> jobList = spcySqsxJobService.listByIds(jobIdList);
            if (CollectionUtils.isNotEmpty(jobList)){
                for (SpcySqsxJob job : jobList) {
                    spcySqsxJobService.removeById(job.getId());
                    SpcyKpNdkpBasic ndkp = spcyKpNdkpBasicService.getById(job.getFpid());
                    if (ndkp != null) {
                        spcyKpNdkpBasicService.removeById(job.getFpid());

                        logNr = "删除年度考评" + job.getFhjmc() + ": ";
                        map = ObjectToMapUtils.objectToMap(ndkp);
                        logNr += LogUtils.MapToStringMethod(map);

                        spcyLogService.addLogs(job.getFhjmc(), logNr, job.getFpid(), LogConstant.LOGS_KP_NDKP, SpcyConstant.LOGS_SYS_TYPE_SC);
                    }
                }
            }
        }

        return HttpResult.ok("删除成功");
    }

    @Log(title = "打开个人登记页面")
    /*@ApiOperation(value = "打开个人登记页面")
    @ApiImplicitParam(name = "ndkpid",value = "年度考评id",required = true)*/
    @GetMapping(value = "openMyNdkpJbxx")
    public HttpResult<?> openMyNdkpJbxx(@RequestParam(value = "ndkpid",defaultValue = "") String ndkpid){
        if (StringUtils.isBlank(ndkpid)){
            return HttpResult.error("年度考评id不能为空");
        }
        SpcyKpNdkpBasic ndkp = spcyKpNdkpBasicService.getById(ndkpid);
        return HttpResult.ok(ndkp);
    }

    @Log(title = "批量查询核查员年度考评信息")
/*    @ApiOperation(value = "批量查询核查员年度考评信息")
    @ApiImplicitParam(name = "ids",value = "年度考评id,id参数逗号拼接",required = true)*/
    @GetMapping(value = "queryBatchHcyNdkpById")
    public HttpResult<?> queryBatchHcyNdkpById(@RequestParam(value = "ids",defaultValue = "")String ids){
        if (StringUtils.isBlank(ids)){
            return HttpResult.error("参数不能为空，请选择数据");
        }
        List<String> idsList = StringPatternUtils.getListByIds(ids);
        List<SpcyKpNdkpBasic> ndkpBasicList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(idsList)){
            ndkpBasicList = spcyKpNdkpBasicService.listByIds(idsList);
        }
        return HttpResult.ok(ndkpBasicList);
    }


    @Log(title = "初始化核查员-年度考评")
    /*@ApiOperation(value = "初始化核查员-年度考评")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "fywflcode",value = "业务分类编码",required = true),
            @ApiImplicitParam(name = "fyear",value = "年度",required = true),
            @ApiImplicitParam(name = "flx",value = "检查员类型",required = true)
    })*/
    @GetMapping(value = "initHcyNdkp")
    public HttpResult<?> initHcyNdkp(@RequestParam(value = "fywflcode",defaultValue = "")String fywflcode,
                                     @RequestParam(value = "flx",defaultValue = "")String flx,
                                     @RequestParam(value = "fyear",defaultValue = "")String fyear){
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser == null){
            return HttpResult.error("登录已失效，请重新登录");
        }

        RsHcyVo rsHcyVo = new RsHcyVo();
        rsHcyVo.setFywflcode(fywflcode);
        rsHcyVo.setFlx(flx);
        rsHcyVo.setFusertype(SpcyConstant.HCY_USERTYPE_JCY);
        List<RsHcyVo> hcyList = spcyRsHcyMapper.getRsHcyList(rsHcyVo);

        if (CollectionUtils.isNotEmpty(hcyList)){
            for (RsHcyVo hcyVo : hcyList){
                String ndkpid = GetUuIdUtils.ReplaceUuId();
                String fuserid = hcyVo.getFuserid();
                if (StringUtils.isBlank(fyear)) {
                    fyear = DateConversionUtils.DateToYear(new Date());
                }

                SpcyKpNdkpBasic ndkp = new SpcyKpNdkpBasic();
                ndkp.setId(ndkpid);
                ndkp.setFuserid(fuserid);
                ndkp.setFusermc(hcyVo.getRealname());
                ndkp.setFywflcode(hcyVo.getFywflcode());
                ndkp.setFgzdw(hcyVo.getFgzdw());
                ndkp.setFyear(fyear);
                ndkp.setFsftjkh(SpcyConstant.NDKP_SFTJKP_YES);
                String zjkssj = fyear+"-01-01";
                String zjjssj = fyear+"-12-31";
                ndkp.setFzjkssj(DateUtil.parse(zjkssj));
                ndkp.setFzjjssj(DateUtil.parse(zjjssj));
                ndkp.setFlczt(SpcyFlowConstant.LCZT_LCZB);

                List<SpcyKpNdkpBasic> ndkpList = spcyKpNdkpBasicService.list(new LambdaQueryWrapper<SpcyKpNdkpBasic>()
                        .eq(StringUtils.isNotBlank(fuserid), SpcyKpNdkpBasic::getFuserid, fuserid)
                        .eq(StringUtils.isNotBlank(fyear), SpcyKpNdkpBasic::getFyear, fyear));
                if (CollectionUtils.isEmpty(ndkpList)) {
                    spcyKpNdkpBasicService.save(ndkp);
//                    ndkpFlowService.jsJoinKpxx(ndkpid); // 计算年度统计数据
                    jcyNdkpFlowService.jsJoinKpxx(ndkpid); // 计算年度统计数据
//                    ndkpFlowService.initJob(ndkpid, currentUser); // 初始化流程
                    jcyNdkpFlowService.initJob(ndkpid, currentUser); // 初始化流程
                }
            }
        }

        return HttpResult.ok();
    }

    @Log(title = "批量发起核查员-年度考评")
    /*@ApiOperation(value = "批量发起核查员-年度考评")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "ids",value = "核查员ids",required = true),
            @ApiImplicitParam(name = "fyear",value = "年度",required = true),
    })*/
    @GetMapping(value = "batchInitHcyNdkp")
    public HttpResult<?> batchInitHcyNdkp(@RequestParam(value = "ids",defaultValue = "")String ids,
                                          @RequestParam(value = "fyear",defaultValue = "")String fyear){
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser == null){
            return HttpResult.error("登录已失效，请重新登录");
        }

        if (StringUtils.isBlank(ids)){
            return HttpResult.error("请选择需要参与年度考评的人员");
        }
        List<String> IdsList = StringPatternUtils.getListByIds(ids);
        List<RsHcyVo> hcyList = spcyRsHcyMapper.getBatchHcyByIds(IdsList);

        if (CollectionUtils.isNotEmpty(hcyList)){
            for (RsHcyVo hcyVo : hcyList){
                String ndkpid = GetUuIdUtils.ReplaceUuId();
                String fuserid = hcyVo.getFuserid();
                if (StringUtils.isBlank(fyear)) {
                    fyear = DateConversionUtils.DateToYear(new Date());
                }

                SpcyKpNdkpBasic ndkp = new SpcyKpNdkpBasic();
                ndkp.setId(ndkpid);
                ndkp.setFuserid(fuserid);
                ndkp.setFusermc(hcyVo.getRealname());
                ndkp.setFywflcode(hcyVo.getFywflcode());
                ndkp.setFgzdw(hcyVo.getFgzdw());
                ndkp.setFyear(fyear);
                String zjkssj = fyear+"-01-01";
                String zjjssj = fyear+"-12-31";
                ndkp.setFzjkssj(DateUtil.parse(zjkssj));
                ndkp.setFzjjssj(DateUtil.parse(zjjssj));
                ndkp.setFsftjkh(SpcyConstant.NDKP_SFTJKP_YES);

                List<SpcyKpNdkpBasic> ndkpList = spcyKpNdkpBasicService.list(new LambdaQueryWrapper<SpcyKpNdkpBasic>()
                        .eq(StringUtils.isNotBlank(fuserid), SpcyKpNdkpBasic::getFuserid, fuserid)
                        .eq(StringUtils.isNotBlank(fyear), SpcyKpNdkpBasic::getFyear, fyear));
                if (CollectionUtils.isEmpty(ndkpList)) {
                    spcyKpNdkpBasicService.save(ndkp);
                    jcyNdkpFlowService.jsJoinKpxx(ndkpid); // 计算年度统计数据
                    jcyNdkpFlowService.initJob(ndkpid, currentUser); // 初始化流程
                }
            }
        }

        return HttpResult.ok("发起成功");
    }



    @Log(title = "导出年度考评基本信息数据-表格excel")
    /*@ApiOperation(value = "导出年度考评基本信息数据-表格excel")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "ids",value = "申请事项id字符逗号分割",required = true)
    })*/
    @GetMapping("/exportNdkpTjData")
    public void exportNdkpTjData(@RequestParam(value = "ids",defaultValue = "")String ids,
                                 SpcyKpNdkpBasic ndkpBasic,
                                 OrderItemVo orderItemVo,
                                 HttpServletResponse response){
        try {
            String name = "exportNdkpData";
            response.setContentType("application/ms-excel;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename="
                    .concat(String.valueOf(URLEncoder.encode(name+".xls", "UTF-8"))));
            OutputStream out = response.getOutputStream();

            List<String> IdsList = StringPatternUtils.getListByIds(ids);
            List<Object> list = new ArrayList<>(); // 导出的数据
            // 多选导出
            if (StringUtils.isNotEmpty(ids)) {
                List<SpcyKpNdkpBasic> ndkpBasicList = spcyKpNdkpBasicService.listByIds(IdsList);
                if (CollectionUtils.isNotEmpty(ndkpBasicList)){
                    for (SpcyKpNdkpBasic e : ndkpBasicList){
                        list.add(e);
                    }
                }
            }else {
                LambdaQueryWrapper<SpcyKpNdkpBasic> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(StringUtils.isNotBlank(ndkpBasic.getFywflcode()),SpcyKpNdkpBasic::getFywflcode,ndkpBasic.getFywflcode());
                queryWrapper.like(StringUtils.isNotBlank(ndkpBasic.getFsftjkh()),SpcyKpNdkpBasic::getFsftjkh,ndkpBasic.getFsftjkh());
                queryWrapper.like(StringUtils.isNotBlank(ndkpBasic.getFyear()),SpcyKpNdkpBasic::getFyear,ndkpBasic.getFyear());
                queryWrapper.like(StringUtils.isNotBlank(ndkpBasic.getFusermc()),SpcyKpNdkpBasic::getFusermc,ndkpBasic.getFusermc());
                queryWrapper.like(StringUtils.isNotBlank(ndkpBasic.getFgzdw()),SpcyKpNdkpBasic::getFgzdw,ndkpBasic.getFgzdw());
                queryWrapper.like(StringUtils.isNotBlank(ndkpBasic.getFsfcykp()),SpcyKpNdkpBasic::getFsfcykp,ndkpBasic.getFsfcykp());
                queryWrapper.like(StringUtils.isNotBlank(ndkpBasic.getFdqssk()),SpcyKpNdkpBasic::getFdqssk,ndkpBasic.getFdqssk());
                queryWrapper.like(StringUtils.isNotBlank(ndkpBasic.getFgrndzjqk()),SpcyKpNdkpBasic::getFgrndzjqk,ndkpBasic.getFgrndzjqk());
                queryWrapper.like(StringUtils.isNotBlank(ndkpBasic.getFndzhkpjg()),SpcyKpNdkpBasic::getFndzhkpjg,ndkpBasic.getFndzhkpjg());
                if (StringUtils.isBlank(ndkpBasic.getFlczt())) {

                }else if (ndkpBasic.getFlczt().equals(SpcyFlowConstant.LCZT_LCBJ)){
                    queryWrapper.like(StringUtils.isNotBlank(ndkpBasic.getFlczt()), SpcyKpNdkpBasic::getFlczt, SpcyFlowConstant.LCZT_LCBJ);
                }else {
                    queryWrapper.isNull(SpcyKpNdkpBasic::getFlczt);
                }
                if (ndkpBasic.getFzjkssj() != null){
                    queryWrapper.le(SpcyKpNdkpBasic::getFzjkssj,ndkpBasic.getFzjkssj());
                }
                if (ndkpBasic.getFzjjssj() != null){
                    queryWrapper.ge(SpcyKpNdkpBasic::getFzjjssj,ndkpBasic.getFzjjssj());
                }
                if (StringUtils.isNotBlank(orderItemVo.getOrderItem())){
                    OrderItemUtils.setOrderItem(queryWrapper, orderItemVo);
                }
                List<SpcyKpNdkpBasic> ndkpBasicList = spcyKpNdkpBasicService.list(queryWrapper);
                // 条件导出
                if (CollectionUtils.isNotEmpty(ndkpBasicList)){
                    for (SpcyKpNdkpBasic e : ndkpBasicList){
                        list.add(e);
                    }
                }
            }
            myExcelUtils.outputListToExclFile(BusinessConstant.IMPORT_NDKP_TJ_XML,list,out); // 导出模板数据成excel
        } catch (Exception e) {
            log.error("导出年度考评统计信息数据出错-{}",e.getMessage());
        }
    }




}
